<template>
  <div>
    <el-button @click="exportPdf">导出pdf</el-button>
    <div ref="pdf" :style="'background: #efefef;width: ' + pageWidth + 'px;'">
      <div class="page-head" :style="'height:' + headerHeight + 'px'">
        <div class="page-head-left"></div>
        <div class="page-head-center">
          <img class="page-head-center-img" src="@/assets/avatar.png" alt="" />
        </div>
        <div class="page-head-right"></div>
      </div>
      <div class="page-foot" :style="'height:' + footerHeight + 'px'">
        <div class="page-foot-left"></div>
        <div class="page-foot-center">
          <span>第</span><span id="pageNumber">{{ currentPage }}</span
          ><span>页</span>
        </div>
        <div class="page-foot-right"></div>
      </div>
      <div
        class="page-home"
        :style="'width: ' + pageWidth + 'px;height:' + pageHeight + 'px'"
      >
        <div class="page-home-top">合同编号：{{ pdfData.baseInfo.code }}</div>
        <div class="page-home-title">{{ pdfData.baseInfo.title }}</div>
        <div class="page-home-info">
          <div class="page-home-info-line">
            <div class="page-home-info-line-tt">甲方：</div>
            <div class="page-home-info-line-txt">
              {{ pdfData.baseInfo.partA }}
            </div>
          </div>
          <div class="page-home-info-line">
            <div class="page-home-info-line-tt">乙方：</div>
            <div class="page-home-info-line-txt">
              {{ pdfData.baseInfo.partB }}
            </div>
          </div>
          <div class="page-home-info-line">
            <div class="page-home-info-line-tt date">签约时间：</div>
            <div class="page-home-info-line-txt date">
              {{ pdfData.baseInfo.date }}
            </div>
          </div>
        </div>
      </div>
      <div class="page-cont">
        <div class="page-cont-title">服务内容</div>
        <div class="page-cont-txt">{{ pdfData.content.main }}</div>
        <div
          class="page-cont-part"
          v-for="(cont, contIndex) of pdfData.content.clause"
          :key="'cont_content_' + contIndex"
        >
          <div class="page-cont-part-tt">
            {{ (contIndex + 1) | turnSort }}、{{ cont.title }}
          </div>
          <div class="page-cont-part-list">
            <div
              class="page-cont-part-list-item"
              v-for="(minor, minorIndex) of cont.minorItems"
              :key="'cont_minor_' + minorIndex"
            >
              <div class="page-cont-part-list-item-txt">{{ minor.origin }}</div>
            </div>
          </div>
        </div>
      </div>
      <!-- <div class="page-cont">
        <div class="page-cont-title">补充协议</div>
        <div class="page-cont-txt">{{ pdfData.supplement.main }}</div>
        <div
          class="page-cont-part"
          v-for="(cont, contIndex) of pdfData.supplement.clause"
          :key="'supple_cont_' + contIndex"
        >
          <div class="page-cont-part-tt">
            {{ (contIndex + 1) | turnSort }}、{{ cont.title }}
          </div>
          <div class="page-cont-part-list">
            <div
              class="page-cont-part-list-item"
              v-for="(minor, minorIndex) of cont.minorItems"
              :key="'supple_minor_' + minorIndex"
            >
              <div class="page-cont-part-list-item-txt">{{ minor.origin }}</div>
            </div>
          </div>
        </div>
      </div> -->
      <!-- <div class="page-cont">
        <div class="page-cont-title">附件</div>
        <div class="page-cont-txt">{{ pdfData.enclosure.main }}</div>
        <div
          class="page-cont-part"
          v-for="(cont, contIndex) of pdfData.enclosure.clause"
          :key="'enclosure_cont_' + contIndex"
        >
          <div class="page-cont-part-tt">
            {{ (contIndex + 1) | turnSort }}、{{ cont.title }}
          </div>
          <div class="page-cont-part-list">
            <div
              class="page-cont-part-list-item"
              v-for="(minor, minorIndex) of cont.minorItems"
              :key="'enclosure_minor_' + minorIndex"
            >
              <div class="page-cont-part-list-item-txt">{{ minor.origin }}</div>
            </div>
          </div>
        </div>
      </div> -->
      <div class="page-part">
        <div class="page-cont-title">部分1</div>
        <div class="page-cont-txt">{{ pdfData.enclosure.main }}</div>
        <div
          class="page-cont-part"
          v-for="(cont, contIndex) of pdfData.enclosure.clause"
          :key="'enclosure_cont_' + contIndex"
        >
          <div class="page-cont-part-tt">
            {{ (contIndex + 1) | turnSort }}、{{ cont.title }}
          </div>
          <div class="page-cont-part-list">
            <div
              class="page-cont-part-list-item"
              v-for="(minor, minorIndex) of cont.minorItems"
              :key="'enclosure_minor_' + minorIndex"
            >
              <div class="page-cont-part-list-item-txt">{{ minor.origin }}</div>
            </div>
          </div>
        </div>
      </div>
      <!-- <div class="page-part">
        <div class="page-cont-title">部分2</div>
        <div class="page-cont-txt">{{ pdfData.enclosure.main }}</div>
        <div
          class="page-cont-part"
          v-for="(cont, contIndex) of pdfData.enclosure.clause"
          :key="'enclosure_cont_' + contIndex"
        >
          <div class="page-cont-part-tt">
            {{ (contIndex + 1) | turnSort }}、{{ cont.title }}
          </div>
          <div class="page-cont-part-list">
            <div
              class="page-cont-part-list-item"
              v-for="(minor, minorIndex) of cont.minorItems"
              :key="'enclosure_minor_' + minorIndex"
            >
              <div class="page-cont-part-list-item-txt">{{ minor.origin }}</div>
            </div>
          </div>
        </div>
      </div> -->
      <!-- <div class="page-part">
        <div class="page-cont-title">部分3</div>
        <div class="page-cont-txt">{{ pdfData.enclosure.main }}</div>
        <div
          class="page-cont-part"
          v-for="(cont, contIndex) of pdfData.enclosure.clause"
          :key="'enclosure_cont_' + contIndex"
        >
          <div class="page-cont-part-tt">
            {{ (contIndex + 1) | turnSort }}、{{ cont.title }}
          </div>
          <div class="page-cont-part-list">
            <div
              class="page-cont-part-list-item"
              v-for="(minor, minorIndex) of cont.minorItems"
              :key="'enclosure_minor_' + minorIndex"
            >
              <div class="page-cont-part-list-item-txt">{{ minor.origin }}</div>
            </div>
          </div>
        </div>
      </div> -->
    </div>
    <br />
    <br />
    <br />
    <div>展示转换之后的的canvas <el-button @click="resetPreview">重置预览</el-button></div>
    <div ref="showpdf"></div>
  </div>
</template>

<script>
import { turnNumToText } from "@/utils";
import pdfData from "./pdfData";
export default {
  data() {
    return {
      pdfData: pdfData, // 准备的模拟数据

      headerHeight: 80,
      footerHeight: 80,

      pageHeight: 0,
      pageWidth: 0,

      headEl: null, // 页眉
      footEl: null, // 页脚
      homeEl: null, // 封面
      els: [[]], // 合同内容

      totalPage: 0, // pdf总页码
      currentPage: 1,
    };
  },
  filters: {
    // 注册一个自定义过滤器，返回数字序号转换的中文序号
    turnSort(num) {
      return turnNumToText(num);
    },
  },
  mounted() {
    // 初始化
    this.init();
  },
  methods: {
    init() {
      this.generatePdfInstance = this.$generatePdfFile({
        headerHeight: this.headerHeight,
        footerHeight: this.footerHeight,
      });
      this.pageHeight = this.generatePdfInstance.options.a4H;
      this.pageWidth = this.generatePdfInstance.options.a4W;
    },
    getEls() {
      this.headEl = this.$refs.pdf.querySelector(".page-head");
      this.footEl = this.$refs.pdf.querySelector(".page-foot");
      this.homeEl = this.$refs.pdf.querySelector(".page-home");
      let part1El = this.$refs.pdf.querySelectorAll(".page-cont");
      let part2El = this.$refs.pdf.querySelectorAll(".page-part");
      let previewBox = this.$refs.showpdf
      this.els = [part1El, part2El];
      return {
        headEl: this.headEl,
        footEl: this.footEl,
        homeEl: this.homeEl,
        pagesEl: this.els,
        homeHeaderFlag: false, // 首页页头是否展示
        homeFooterFlag: false, // 首页页尾是否展示
        pagesHeaderFlag: true, // part/pages/content 部分页头是否展示
        pagesFooterFlag: true, // part/pages/content 部分页尾是否展示
        // previewBox
      };
    },
    async exportPdf() {
      let elsOptions = this.getEls();
      let info = await this.generatePdfInstance.generateFile(elsOptions);
      console.log(info)
      // let uploadInfo = await this.generatePdfInstance.uploadFile({
      //   url: this.$api.upload.upload_file,
      //   filename: "我晕",
      // });
      // console.log('uploadInfo',uploadInfo)
      this.generatePdfInstance.exportFile('大橙子')
    },
    resetPreview(){
      this.generatePdfInstance.resetPreviewBox()
    }
  },
};
</script>

<style lang="scss" scoped>
.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  color: #cccccc;
  font-size: 14px;
  .page-head-left {
  }
  .page-head-center {
    box-sizing: border-box;
    padding-bottom: 6px;
    .page-head-center-img {
      width: 24px;
      height: 24px;
    }
  }
  .page-head-right {
  }
}
.page-foot {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  color: #cccccc;
  font-size: 14px;
  .page-foot-left {
  }
  .page-foot-center {
  }
  .page-foot-right {
  }
}
.page-home {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  color: #333333;
  font-size: 16px;
  .page-home-top {
    width: 100%;
    box-sizing: border-box;
    text-align: right;
    padding-top: 24px;
    padding-right: 24px;
  }
  .page-home-title {
    width: 100%;
    height: 50%;
    box-sizing: border-box;
    font-size: 32px;
    font-weight: bold;
    text-align: center;
    letter-spacing: 6px;
  }
  .page-home-info {
    width: 40%;
    box-sizing: border-box;
    padding-bottom: 24px;
    font-size: 18px;
    .page-home-info-line {
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
      padding-top: 12px;
      .page-home-info-line-tt {
        width: 60px;
        &.date {
          width: 100px;
        }
      }
      .page-home-info-line-txt {
        width: calc(100% - 60px);
        &.date {
          width: calc(100% - 100px);
        }
      }
    }
  }
}
.page-cont,
.page-part {
  font-size: 14px;
  color: rgb(51, 51, 51);
  box-sizing: border-box;
  padding: 0 24px;
  .page-cont-title {
    font-size: 18px;
    text-align: center;
    font-weight: bold;
    padding: 24px 0px;
  }
  .page-cont-txt {
    text-indent: 2em;
    line-height: 2;
  }
  .page-cont-part {
    .page-cont-part-tt {
      font-weight: bold;
      line-height: 2;
    }
    .page-cont-part-list {
      .page-cont-part-list-item {
        .page-cont-part-list-item-txt {
          text-indent: 2em;
          line-height: 2;
        }
      }
    }
  }
}
</style>